package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.contract.bean.ReleaseObjectBean;
import com.wwwarehouse.contract.event.VerifyIsPublishEvent;

import java.util.ArrayList;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/10/18.
 *         <p/>
 */

public class SelectObjAdapter extends RecyclerView.Adapter<SelectObjAdapter.ViewHolder> {

    private Context mContext = null;
    private int mHeight = 0;
    private int mWidth = 0;
    private OnItemClickListener mListener = null;
    private ArrayList<ReleaseObjectBean.BusinessListBean> tagList = new ArrayList<>();
    private int pos;
    private ArrayList<ReleaseObjectBean.BusinessListBean> selectList;
    private int numOfPage ;
    public SelectObjAdapter(Context context, ArrayList<ReleaseObjectBean.BusinessListBean> list, int height, int Width, int numOfPage, int pos) {
        this.mContext = context;
        this.mHeight = height;
        this.mWidth = Width;
        this.numOfPage = numOfPage;
        this.tagList = list;
        this.pos = pos;
        this.selectList = new ArrayList<>();

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_contract_select_obj_adapter, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
        params.height = (mHeight  - ConvertUtils.dip2px(mContext,(numOfPage+1)*14)- ConvertUtils.dip2px(mContext,50))/ numOfPage; //RecyclerView高度减去item间距再减去距离按钮的高度
        //获取当前控件的布局对象
//        params.width = (int) ((mWidth - 4* ConvertUtils.dip2px(mContext, 10)) /3L);//设置当前控件布局的宽度和margin
        holder.itemView.setLayoutParams(params);
//        holder.mTextView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                holder.mTv.setSelected(!holder.mTv.isSelected());
//                if (mListener != null) {
//                    mListener.ItemClickListener(position, v, mList.get(position));
//                }
//            }
//        });
        holder.mCorporateName.setText(tagList.get(position).getBuName());
        holder.mName.setText(tagList.get(position).getOtherSalesMans().get(0).getSalesManName());
        holder.itemView.setTag(tagList.get(position));
        if (tagList.get(position).isSelect()){
            holder.mCorporateName.setTextColor(mContext.getResources().getColor(R.color.common_color_c1_337cff));
            holder.mImgCheck.setVisibility(View.VISIBLE);
        } else {
            holder.mCorporateName.setTextColor(mContext.getResources().getColor(R.color.common_color_c5_45494e));
            holder.mImgCheck.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new VerifyIsPublishEvent(tagList.get(position).getBuId(),pos,position,tagList.get(position).getBuName(),tagList.get(position).isSelect()));
//
            }
        });

    }

    @Override
    public int getItemCount() {
        return tagList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mCorporateName;
        public TextView mName;
        public ImageView mImgCheck;

        public ViewHolder(View view) {
            super(view);
            mCorporateName = (TextView) view.findViewById(R.id.corporate_name);
            mName = (TextView) view.findViewById(R.id.name);
            mImgCheck = (ImageView) view.findViewById(R.id.img_check);
        }
    }
    public interface OnItemClickListener {
        void ItemClickListener(ArrayList<ReleaseObjectBean.BusinessListBean> tagList);
    }

    public ArrayList<ReleaseObjectBean.BusinessListBean> getSelectData(){
        selectList.clear();
        for (int i = 0; i < tagList.size(); i++) {
            if (tagList.get(i).isSelect()) {
                selectList.add(tagList.get(i));
            }
        }
        return selectList;
    }
}
